home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / arp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-27  |  11.9 KB  |  394 lines

  1. /* Address Resolution Protocol (ARP) functions. Sits between IP and
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  * Level 2, mapping IP to Level 2 addresses for all outgoing datagrams.
  4.  *
  5.  * Mods by G1EMM
  6.  *
  7.  * Mods by SM6RPZ
  8.  * 1992-05-28 - Added interface to arp_lookup() and arp_add().
  9.  * 1992-07-07 - Added arp_timeout(). This function now works according to the
  10.  *        last page in RFC826. When an ARP entry times out we will first
  11.  *        try an ARP reply before deleting the entry.
  12.  * 1992-07-08 - We will update the ARP table and create new entries each time
  13.  *        we hear an ARP request message on the channel. By doing so we
  14.  *        will gain knowledge of other stations hardware addresses
  15.  *        whithout sending a lot of ARP requests. If we are running RSPF,
  16.  *        it will be informed about these "findings".
  17.  * 1992-07-11 - Datagrams to be sent while awaiting an resolution will be put
  18.  *        on a queue. The length of this queue in datagrams is defined
  19.  *        in arp.h (ARP_QUEUE).
  20.  *
  21.  * 1992-10-27 The above 3 behaviours are now configurable via additional
  22.  *      arp subcommands - WG7J
  23.  *
  24.  */
  25.  
  26. #include "global.h"
  27. #include "config.h"
  28. #include "mbuf.h"
  29. #include "timer.h"
  30. #include "iface.h"
  31. #include "enet.h"
  32. #include "ax25.h"
  33. #include "icmp.h"
  34. #include "ip.h"
  35. #include "arp.h"
  36. #include "icmp.h"
  37. #include "rspf.h"
  38.  
  39. extern int Maxarpq;
  40.  
  41. static void arp_output __ARGS((struct iface *iface,int16 hardware,int32 target,char *hw_addr));
  42. static void arp_timeout __ARGS((void *p));  /* sm6rpz */
  43.  
  44. /* Hash table headers */
  45. struct arp_tab *Arp_tab[HASHMOD];
  46. struct arp_stat Arp_stat;
  47.  
  48. /* Resolve an IP address to a hardware address; if not found,
  49.  * initiate query and return NULLCHAR.  If an address is returned, the
  50.  * interface driver may send the packet; if NULLCHAR is returned,
  51.  * res_arp() will have saved the packet on its pending queue,
  52.  * so no further action (like freeing the packet) is necessary.
  53.  */
  54. char *
  55. res_arp(iface,hardware,target,bp)
  56. struct iface *iface;    /* Pointer to interface block */
  57. int16 hardware;        /* Hardware type */
  58. int32 target;        /* Target IP address */
  59. struct mbuf *bp;    /* IP datagram to be queued if unresolved */
  60. {
  61.     register struct arp_tab *arp;
  62.     struct ip ip;
  63.  
  64.     if((arp = arp_lookup(hardware,target,iface)) != NULLARP && arp->state == ARP_VALID)
  65.         return arp->hw_addr;
  66.     if(arp != NULLARP){
  67.         if(len_q(arp->pending) > Maxarpq) {
  68.             /* Earlier packets are already pending, kick
  69.               * this one back as a source quench
  70.              */
  71.             ntohip(&ip,&bp);
  72.             icmp_output(&ip,bp,ICMP_QUENCH,0,NULL);
  73.             free_p(bp);
  74.         } else
  75.             enqueue(&arp->pending,bp);
  76.     } else {
  77.         /* Create an entry and put the datagram on the
  78.          * queue pending an answer
  79.          */
  80.         arp = arp_add(target,hardware,NULLCHAR,0,iface);
  81.         enqueue(&arp->pending,bp);
  82.         arp_output(iface,hardware,target,NULLCHAR);
  83.     }
  84.     return NULLCHAR;
  85. }
  86.  
  87. /* Handle incoming ARP packets. This is almost a direct implementation of
  88.  * the algorithm on page 5 of RFC 826, except for:
  89.  * 1. Outgoing datagrams to unresolved addresses are kept on a queue
  90.  *    pending a reply to our ARP request.
  91.  * 2. The names of the fields in the ARP packet were made more mnemonic.
  92.  * 3. Requests for IP addresses listed in our table as "published" are
  93.  *    responded to, even if the address is not our own.
  94.  */
  95. void
  96. arp_input(iface,bp)
  97. struct iface *iface;
  98. struct mbuf *bp;
  99. {
  100.     struct arp arp;
  101.     struct arp_tab *ap;
  102.     struct arp_type *at;
  103.     int i;
  104.     
  105.     Arp_stat.recv++;
  106.     if(ntoharp(&arp,&bp) == -1)    /* Convert into host format */
  107.         return;
  108.     if(arp.hardware >= NHWTYPES){
  109.         /* Unknown hardware type, ignore */
  110.         Arp_stat.badtype++;
  111.         return;
  112.     }
  113.     at = &Arp_type[arp.hardware];
  114.     if(arp.protocol != at->iptype){
  115.         /* Unsupported protocol type, ignore */
  116.         Arp_stat.badtype++;
  117.         return;
  118.     }
  119.     if(uchar(arp.hwalen) > MAXHWALEN || uchar(arp.pralen) != sizeof(int32)){
  120.         /* Incorrect protocol addr length (different hw addr lengths
  121.          * are OK since AX.25 addresses can be of variable length)
  122.          */
  123.         Arp_stat.badlen++;
  124.         return;
  125.     }
  126.     if(arp.sprotaddr == 0L || arp.tprotaddr == 0L){
  127.         /* We are going to dead-end references for [0.0.0.0], since
  128.          * experience shows that these cause total lock up -- N1BEE
  129.          */
  130.         Arp_stat.badaddr++;
  131.         return;
  132.     }
  133.     if(memcmp(arp.shwaddr,at->bdcst,at->hwalen) == 0){
  134.         /* This guy is trying to say he's got the broadcast address! */
  135.         Arp_stat.badaddr++;
  136.         return;
  137.     }
  138.     /* Try to refine ARP according to section 5.7 in Douglas E.
  139.      * Comers book "Internetworking With TCP/IP", 2nd ed. page 77.
  140.      * I.e we will use all ARP-packets we can see thereby lessen
  141.      * the ARP traffic a little. -- sm6rpz
  142.      */
  143.     if(((ap = arp_lookup(arp.hardware,arp.sprotaddr,iface)) != NULLARP
  144.         && dur_timer(&ap->timer) != 0)
  145.        || ( (iface->flags & ARP_EAVESDROP) &&
  146.             ap == NULLARP && arp.opcode != REVARP_REQUEST)
  147.       ) {
  148.     ap = arp_add(arp.sprotaddr,arp.hardware,arp.shwaddr,0,iface);
  149.     }
  150.     /* See if we're the address they're looking for */
  151.     if(ismyaddr(arp.tprotaddr) != NULLIF){
  152.         if(ap == NULLARP)   /* Only if not already in the table */
  153.             arp_add(arp.sprotaddr,arp.hardware,arp.shwaddr,0,iface);
  154.  
  155.         if(arp.opcode == ARP_REQUEST){
  156.             /* Swap sender's and target's (us) hardware and protocol
  157.              * fields, and send the packet back as a reply
  158.              */
  159.             memcpy(arp.thwaddr,arp.shwaddr,(int16)uchar(arp.hwalen));
  160.             /* Mark the end of the sender's AX.25 address
  161.              * in case he didn't
  162.              */
  163.             if(arp.hardware == ARP_AX25)
  164.                 arp.thwaddr[uchar(arp.hwalen)-1] |= E;
  165.  
  166.             memcpy(arp.shwaddr,iface->hwaddr,at->hwalen);
  167.             arp.tprotaddr = arp.sprotaddr;
  168.             arp.sprotaddr = iface->addr;
  169.             arp.opcode = ARP_REPLY;
  170.             if((bp = htonarp(&arp)) == NULLBUF)
  171.                 return;
  172.  
  173.             if(iface->forw != NULLIF)
  174.                 (*iface->forw->output)(iface->forw,
  175.                  arp.thwaddr,iface->forw->hwaddr,at->arptype,bp);
  176.             else 
  177.                 (*iface->output)(iface,arp.thwaddr,
  178.                  iface->hwaddr,at->arptype,bp);
  179.             Arp_stat.inreq++;
  180. #ifdef    RSPF
  181.             /* Do an RSPF upcall */
  182.             rspfarpupcall(arp.tprotaddr,arp.hardware,NULLIF);
  183. #endif    /* RSPF*/
  184.         } else {
  185.             Arp_stat.replies++;
  186. #ifdef    RSPF
  187.             /* Do an RSPF upcall */
  188.             rspfarpupcall(arp.sprotaddr,arp.hardware,iface);
  189. #endif    /* RSPF*/
  190.         }
  191.     } else if(arp.opcode == ARP_REQUEST
  192.      && (ap = arp_lookup(arp.hardware,arp.tprotaddr,iface)) != NULLARP
  193.      && ap->pub){
  194.         /* Otherwise, respond if the guy he's looking for is
  195.          * published in our table.
  196.          */
  197.         memcpy(arp.thwaddr,arp.shwaddr,(int16)uchar(arp.hwalen));
  198.         memcpy(arp.shwaddr,ap->hw_addr,at->hwalen);
  199.         arp.tprotaddr = arp.sprotaddr;
  200.         arp.sprotaddr = ap->ip_addr;
  201.         arp.opcode = ARP_REPLY;
  202.         if((bp = htonarp(&arp)) == NULLBUF)
  203.             return;
  204.         if(iface->forw != NULLIF)
  205.             (*iface->forw->output)(iface->forw,
  206.              arp.thwaddr,iface->forw->hwaddr,at->arptype,bp);
  207.         else 
  208.             (*iface->output)(iface,arp.thwaddr,
  209.              iface->hwaddr,at->arptype,bp);
  210.         Arp_stat.inreq++;
  211.     } else if(arp.opcode == REVARP_REQUEST){
  212.         for(i=0;i<HASHMOD;i++)
  213.             for(ap = Arp_tab[i];ap != NULLARP;ap = ap->next)
  214.                 if(memcmp(ap->hw_addr,arp.thwaddr,at->hwalen) == 0)
  215.                     goto found;
  216.     found:    if(ap != NULLARP && ap->pub){
  217.             memcpy(arp.shwaddr,iface->hwaddr,at->hwalen);
  218.             arp.tprotaddr = ap->ip_addr;
  219.             arp.sprotaddr = iface->addr;
  220.             arp.opcode = REVARP_REPLY;
  221.             if((bp = htonarp(&arp)) == NULLBUF)
  222.                 return;
  223.             if(iface->forw != NULLIF)
  224.                 (*iface->forw->output)(iface->forw,
  225.                  arp.thwaddr,iface->forw->hwaddr,REVARP_TYPE,bp);
  226.             else 
  227.                 (*iface->output)(iface,arp.thwaddr,
  228.                  iface->hwaddr,REVARP_TYPE,bp);
  229.             Arp_stat.inreq++;
  230.         }
  231.     }
  232. }
  233. /* Add an IP-addr / hardware-addr pair to the ARP table */
  234. struct arp_tab *
  235. arp_add(ipaddr,hardware,hw_addr,pub,iface)
  236. int32 ipaddr;        /* IP address, host order */
  237. int16 hardware;        /* Hardware type */
  238. char *hw_addr;        /* Hardware address, if known; NULLCHAR otherwise */
  239. int pub;        /* Publish this entry? */
  240. struct iface *iface;
  241. {
  242.     struct mbuf *bp;
  243.     register struct arp_tab *ap;
  244.     struct arp_type *at;
  245.     unsigned hashval;
  246.  
  247.     if(hardware >=NHWTYPES)
  248.         return NULLARP;    /* Invalid hardware type */
  249.     at = &Arp_type[hardware];
  250.  
  251.     if((ap = arp_lookup(hardware,ipaddr,iface)) == NULLARP){
  252.         /* New entry */
  253.         ap = (struct arp_tab *)callocw(1,sizeof(struct arp_tab));
  254.         ap->hw_addr = mallocw(at->hwalen);
  255.         ap->timer.func = arp_timeout;
  256.         ap->timer.arg = ap;
  257.         ap->hardware = hardware;
  258.         ap->ip_addr = ipaddr;
  259.         ap->iface = iface;
  260.  
  261.         /* Put on head of hash chain */
  262.         hashval = hash_ip(ipaddr);
  263.         ap->prev = NULLARP;
  264.         ap->next = Arp_tab[hashval];
  265.         Arp_tab[hashval] = ap;
  266.         if(ap->next != NULLARP){
  267.             ap->next->prev = ap;
  268.         }
  269.     }
  270.     if(hw_addr == NULLCHAR){
  271.         /* Await response */
  272.         ap->state = ARP_PENDING;
  273.         set_timer(&ap->timer,Arp_type[hardware].pendtime * 1000L);
  274.     } else {
  275.         /* Response has come in, update entry and run through queue */
  276.         ap->state = ARP_VALID;
  277.         set_timer(&ap->timer,ARPLIFE*1000L);
  278.         memcpy(ap->hw_addr,hw_addr,at->hwalen);
  279.         ap->pub = pub;
  280.         while((bp = dequeue(&ap->pending)) != NULLBUF)
  281.             ip_route(NULLIF,bp,0);
  282.     }
  283.     start_timer(&ap->timer);
  284.     return ap;
  285. }
  286.  
  287. /* Remove an entry from the ARP table */
  288. void
  289. arp_drop(p)
  290. void *p;
  291. {
  292.     register struct arp_tab *ap;
  293.  
  294.     ap = (struct arp_tab *)p;
  295.     if(ap == NULLARP)
  296.         return;
  297.     stop_timer(&ap->timer);    /* Shouldn't be necessary */
  298.     if(ap->next != NULLARP)
  299.         ap->next->prev = ap->prev;
  300.     if(ap->prev != NULLARP)
  301.         ap->prev->next = ap->next;
  302.     else
  303.         Arp_tab[hash_ip(ap->ip_addr)] = ap->next;
  304.     free_q(&ap->pending);
  305.     free(ap->hw_addr);
  306.     free((char *)ap);
  307. }
  308.  
  309. /* Look up the given IP address in the ARP table */
  310. struct arp_tab *
  311. arp_lookup(hardware,ipaddr,iface)
  312. int16 hardware;
  313. int32 ipaddr;
  314. struct iface *iface;
  315. {
  316.     register struct arp_tab *ap;
  317.  
  318.     for(ap = Arp_tab[hash_ip(ipaddr)]; ap != NULLARP; ap = ap->next){
  319.         if(ap->ip_addr == ipaddr && ap->hardware == hardware \
  320.             && ap->iface == iface)
  321.             break;
  322.     }
  323.     return ap;
  324. }
  325. /* Send an ARP request to resolve IP address target_ip */
  326. static void
  327. arp_output(iface,hardware,target,hw_addr)
  328. struct iface *iface;
  329. int16 hardware;
  330. int32 target;
  331. char *hw_addr;
  332. {
  333.     struct arp arp;
  334.     struct mbuf *bp;
  335.     struct arp_type *at;
  336.  
  337.     at = &Arp_type[hardware];
  338.     if(iface->output == NULLFP)
  339.         return;
  340.     
  341.     arp.hardware = hardware;
  342.     arp.protocol = at->iptype;
  343.     arp.hwalen = at->hwalen;
  344.     arp.pralen = sizeof(int32);
  345.     arp.opcode = ARP_REQUEST;
  346.     memcpy(arp.shwaddr,iface->hwaddr,at->hwalen);
  347.     arp.sprotaddr = iface->addr;
  348.     memset(arp.thwaddr,0,at->hwalen);
  349.     arp.tprotaddr = target;
  350.     if((bp = htonarp(&arp)) == NULLBUF)
  351.         return;
  352.     if(hw_addr == NULLCHAR)
  353.         (*iface->output)(iface,at->bdcst,iface->hwaddr,at->arptype,bp);
  354.     else
  355.         (*iface->output)(iface,hw_addr,iface->hwaddr,at->arptype,bp);
  356.     Arp_stat.outreq++;
  357. }
  358.  
  359. /* Called when an ARP entry times out. If an entry has been a valid
  360.  * one we will send out an ARP reply. If this one does not succed,
  361.  * the ARP entry will be dropped. -- sm6rpz
  362.  *
  363.  * Made configurable by WG7J.
  364.  */
  365. static void
  366. arp_timeout(p)
  367. void *p;
  368. {
  369.     struct arp_type *at;
  370.     char *hwaddr;
  371.     struct arp_tab *ap = (struct arp_tab *)p;
  372.     
  373.     if(ap == NULLARP)
  374.         return;
  375.     stop_timer(&ap->timer);
  376.     /* Check to see if the timer is set to ARPLIFE or pendtime. Set_timer()
  377.      * adds at least one tick so we must do a little more flexible check.
  378.      */
  379.     if( (ap->iface->flags & ARP_KEEPALIVE) &&
  380.         (dur_timer(&ap->timer) >= ARPLIFE * 1000L)) {
  381.         at = &Arp_type[ap->hardware];
  382.         set_timer(&ap->timer,at->pendtime * 1000L);
  383.         /* timer functions should NEVER call blocked allocs ! - WG7J */
  384.         if((hwaddr = mallocw(at->hwalen)) != 0) {
  385.             memcpy(hwaddr,ap->hw_addr,at->hwalen);
  386.             arp_output(ap->iface,ap->hardware,ap->ip_addr,hwaddr);
  387.             free(hwaddr);           /* clean up */
  388.         }
  389.         start_timer(&ap->timer);
  390.     } else
  391.         arp_drop(ap);
  392. }
  393.  
  394.